package com.itheima.controller;

import com.itheima.domian.User;
import com.itheima.service.UserService;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Autowired
    private UserService userService;


    @RequestMapping("/login")
    public String login(User user, HttpServletRequest request){
        try {
            User u=userService.login(user);
            /*
            用户账号和密码是否查询出用户信息
                是：将用户信息存入Session，并跳转到后台首页
                否：Request域中添加提示信息，并转发到登录页面
             */
            if(u!=null){
                request.getSession().setAttribute("USER_SESSION",u);
                return "redirect:/admin/main.jsp";
            }
            request.setAttribute("msg","用户名或密码错误");
            return  "forward:/admin/login.jsp";
        }catch(Exception e){
            e.printStackTrace();
            request.setAttribute("msg","系统错误");
            return  "forward:/admin/login.jsp";
        }
    }
    @RequestMapping("/logoff")
    public String logout( HttpServletRequest request){
        try {
            HttpSession session = request.getSession();
            //销毁Session
            session.invalidate();
            return  "forward:/admin/login.jsp";
        }catch(Exception e){
            e.printStackTrace();
            request.setAttribute("msg","系统错误");
            return  "forward:/admin/login.jsp";
        }
    }

    @RequestMapping("/selectAllUser")
    public ModelAndView selectAllUser(){
        int pageNum = 1;
        int pageSize = 5;
        PageResult pageResult = userService.selectAllUser(pageNum, pageSize);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("alluser");
        modelAndView.addObject("pageResult", pageResult);
        return modelAndView;
    }
    @RequestMapping("/addUser")
    public ModelAndView addUser(User user,HttpServletRequest request){
        userService.addUser(user);
        request.setAttribute("adduser","增加用户信息成功");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("adduser");
        //将当前查询的控制器路径返回到页面，页码变化时继续向该路径发送请求
        modelAndView.addObject("gourl", request.getRequestURI());
        return modelAndView;
    }
    @RequestMapping("/updateUser")
    public ModelAndView updateUser(User user,HttpServletRequest request){
        userService.updateUser(user);
        request.setAttribute("updateuser","修改用户信息成功");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("updateuser");
        //将当前查询的控制器路径返回到页面，页码变化时继续向该路径发送请求
        modelAndView.addObject("gourl", request.getRequestURI());
        return modelAndView;
    }
    @RequestMapping("/deleteUser")
    public ModelAndView deleteUser(User user,HttpServletRequest request){
        userService.deleteUser(user);
        request.setAttribute("deleteuser","删除用户信息成功");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("deleteuser");
        //将当前查询的控制器路径返回到页面，页码变化时继续向该路径发送请求
        modelAndView.addObject("gourl", request.getRequestURI());
        return modelAndView;
    }
}

